home *** CD-ROM | disk | FTP | other *** search
Java Source | 1996-11-07 | 2.8 KB | 156 lines | [TEXT/CWIE] |
- import java.awt.*;
- import java.util.*;
-
- abstract class Shape
- {
- boolean highlighted;
- ShapeCanvas shapeCanvas;
- int shapeX, shapeY, shapeWidth, shapeHeight;
- Rectangle boundsRect;
-
- Shape( ShapeCanvas canv, int x, int y, int width, int height )
- {
- shapeCanvas = canv;
- highlighted = false;
-
- shapeX = x;
- shapeY = y;
- shapeWidth = width;
- shapeHeight = height;
-
- boundsRect = new Rectangle( x, y, width, height );
- }
-
- abstract public void draw( Graphics g );
-
- public void setHighlight( boolean newHighlight )
- {
- highlighted = newHighlight;
- }
-
- public boolean isHighlighted()
- {
- return highlighted;
- }
-
- public boolean isPointInShape( int x, int y )
- {
- return boundsRect.inside( x, y );
- }
- }
-
- class RectShape extends Shape
- {
- RectShape( ShapeCanvas canv, int x, int y, int width, int height )
- {
- super( canv, x, y, width, height );
- }
-
- public void draw( Graphics g )
- {
- if ( isHighlighted() )
- {
- g.setColor( Color.black );
- g.fillRect( shapeX, shapeY, shapeWidth, shapeHeight );
- g.setColor( Color.red );
- g.fillRect( shapeX+2, shapeY+2, shapeWidth-4, shapeHeight-4 );
- }
- else
- {
- g.setColor( Color.red );
- g.fillRect( shapeX, shapeY, shapeWidth, shapeHeight );
- }
- }
- }
-
- class ShapeCanvas extends Canvas
- {
- Vector shapes;
- Shape curShape;
-
- ShapeCanvas( int width, int height )
- {
- shapes = new Vector();
- curShape = null;
-
- setBackground( Color.yellow );
-
- resize( width, height );
- }
-
- public void addShape( Shape newShape )
- {
- shapes.addElement( newShape );
- }
-
- public void paint( Graphics g )
- {
- for ( Enumeration e = shapes.elements(); e.hasMoreElements(); )
- {
- Shape s = (Shape)e.nextElement();
- s.draw( g );
- }
- }
-
- public Shape findInShapeList( int x, int y )
- {
- for ( Enumeration e = shapes.elements(); e.hasMoreElements(); )
- {
- Shape s = (Shape)e.nextElement();
-
- if ( s.isPointInShape( x, y ) )
- {
- s.setHighlight( ! s.isHighlighted() );
- s.draw( getGraphics() );
- // repaint();
- return s;
- }
- }
-
- return null;
- }
-
- public void update (Graphics g)
- {
- paint(g);
- }
-
- public boolean mouseDown( Event e, int x, int y )
- {
- curShape = findInShapeList( x, y );
-
- return true;
- }
- }
-
- public class ShapeWorld extends java.applet.Applet
- {
- ShapeCanvas sCanvas;
- final int shapeWidth = 20;
- final int shapeHeight = 20;
-
- public void init()
- {
- int x, y;
-
- sCanvas = new ShapeCanvas( 440, 290 );
- add( sCanvas );
-
- Random ran = new Random();
- Rectangle b = sCanvas.bounds();
-
- for ( int i=1; i<=10; i++ )
- {
- x = b.x + (int)((float)(b.width) * ran.nextFloat() );
- if ( x > b.x + b.width - shapeWidth )
- x -= shapeWidth;
-
- y = b.y + (int)((float)(b.height) * ran.nextFloat() );
- if ( y > b.y + b.height - shapeHeight )
- y -= shapeHeight;
-
- RectShape r = new RectShape( sCanvas, x, y, shapeWidth, shapeHeight );
- sCanvas.addShape( r );
- }
- }
- }